package com.lanou.musicproject.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.LoginActivity;
import com.lanou.musicproject.activity.ScanActivity;
import com.lanou.musicproject.utils.MyConfig;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 王世美 on 15/10/20.
 * 我的页面
 */
public class MyFragment extends Fragment implements View.OnClickListener {
    private Button scanBtn;
    private ImageView loadingImage1;
    private ImageView loadingImage2;
    private CircleImageView heardIV;
    private TextView hintTV,count_local;
    private Handler myHandler = new Handler();
    AnimationSet animationSet1;
    AnimationSet animationSet2;
    private String name, img;
    int i = 1;
    private Context myContext;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        IntentFilter countFilter = new IntentFilter();
        countFilter.addAction("countAction");
        getActivity().registerReceiver(countReceiver,countFilter);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view_my = inflater.inflate(R.layout.fragment_my,null);
        scanBtn = (Button) view_my.findViewById(R.id.scanBtn);
        scanBtn.setOnClickListener(this);
        loadingImage1 = (ImageView) view_my.findViewById(R.id.loadingImage1);
        loadingImage2 = (ImageView) view_my.findViewById(R.id.loadingImage2);
        heardIV = (CircleImageView) view_my.findViewById(R.id.heardIV);
        hintTV = (TextView) view_my.findViewById(R.id.hintTV);
        count_local = (TextView) view_my.findViewById(R.id.count_local);
        hintTV.setOnClickListener(this);
        this.animation();

        return view_my;
    }

    //广播的接收 收到扫描的数目
    private BroadcastReceiver countReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           int c = intent.getIntExtra("countLocal",0);
            count_local.setText(c + "首歌已经下载完成");
        }
    };

    private void animation() {
        // 第一个动画
        animationSet1 = new AnimationSet(true);
        TranslateAnimation translateAnimation1 = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,
                -1f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f);
        translateAnimation1.setDuration(2000);
        AlphaAnimation alphaAnimation1 = new AlphaAnimation(1,0);
        alphaAnimation1.setDuration(2800);
        animationSet1.addAnimation(translateAnimation1);
        animationSet1.addAnimation(alphaAnimation1);
        // 第二个动画
        animationSet2 = new AnimationSet(true);
        TranslateAnimation translateAnimation2 = new TranslateAnimation(
           Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-1.17f,
                Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f
        );
        translateAnimation2.setDuration(3000);
        AlphaAnimation alphaAnimation2 = new AlphaAnimation(0,1);
        alphaAnimation2.setDuration(3000);
        animationSet2.addAnimation(translateAnimation2);
        animationSet2.addAnimation(alphaAnimation2);
        // 开始播放
        // loadingImage1.startAnimation(animationSet1);
        // loadingImage2.startAnimation(animationSet2);
        myHandler.post(myThread);
    }

    Runnable myThread = new Runnable() {
        @Override
        public void run() {
            if (i == 7) {
                i = 0;
                loadingImage2.setImageResource(
                        R.drawable.monday);
                //myHandler.removeCallbacks(myThread);
                //return;//可以从这里跳到你需要的Activity
            } else {
                switch (i) {
                    case 1:
                        loadingImage1.setImageResource(
                                R.drawable.monday);
                        loadingImage2.setImageResource(
                                R.drawable.tuesday);
                        break;
                    case 2:
                        loadingImage1.setImageResource(
                                R.drawable.tuesday);
                        loadingImage2.setImageResource(
                                R.drawable.wednesday);
                        break;
                    case 3:
                        loadingImage1.setImageResource(
                                R.drawable.wednesday);
                        loadingImage2.setImageResource(
                                R.drawable.thursday);
                        break;
                    case 4:
                        loadingImage1.setImageResource(
                                R.drawable.thursday);
                        loadingImage2.setImageResource(
                                R.drawable.friday);
                        break;
                    case 5:
                        loadingImage1.setImageResource(
                                R.drawable.friday);
                        loadingImage2.setImageResource(
                                R.drawable.saturdayandsunday);
                        break;
                }
                if (i != 6) {
                    loadingImage1.startAnimation(animationSet1);
                    loadingImage2.startAnimation(animationSet2);
                } else {
                    loadingImage1.setImageResource(
                            R.drawable.saturdayandsunday);
                }
                i = i + 1;
            }
            if(i!=7)
            {
                myHandler.postDelayed(myThread, 3000);
            }
            else
            {
                myHandler.postDelayed(myThread, 1000);
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.scanBtn:

                Intent intent = new Intent(getActivity(), ScanActivity.class);
                startActivity(intent);

                break;
            case R.id.hintTV:
                Intent intent1 = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent1);

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        name = MyConfig.getSharePreStr(getActivity(), "user", "screen_name");
        img = MyConfig.getSharePreStr(getActivity(), "img", "profile_image_url");
        if (name != null && !name.equals("0") && img != null && !img.equals("0")) {
            hintTV.setText(name);
            Picasso.with(getActivity()).load(img).into(heardIV);
        } else {
            hintTV.getText();
            heardIV.getDrawable();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(countReceiver);
    }
}
